import datetime
from django.db import models
from user.models import UserModel, BaseModel, DeptModel


# 审批层次
class LevelModel(models.Model):
    name = models.CharField(verbose_name="审批称此名称", max_length=50)
    level = models.CharField(verbose_name="审批层次", max_length=50)
    currentvetting = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="审批人")
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)
    level_self = models.ForeignKey("self", on_delete=models.CASCADE, verbose_name="审批子自联", null=True, blank=True)

    class Meta:
        db_table = "level_tb"
        verbose_name = "审批表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 类别
class TypeModel(models.Model):
    name = models.CharField(verbose_name="类型名称", max_length=35)
    template_id = models.IntegerField(null=True, verbose_name="模板名称")
    lastervisename = models.ForeignKey(UserModel, on_delete=models.CASCADE, null=True, blank=True, verbose_name="最后的修改人")
    upd_time = models.DateTimeField(verbose_name="审批时间", auto_now_add=True, null=True, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "type_tb"
        verbose_name = "类别表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 流程表
class FlowModel(models.Model):
    flow_name = models.CharField(verbose_name="流程名称", max_length=25)
    peocess = models.CharField(verbose_name="流程说明", max_length=100,null=True)
    currentvetting = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="审批人", null=True)
    dept = models.ForeignKey(DeptModel, on_delete=models.CASCADE, verbose_name="部门id", null=True, blank=True)
    is_enable = models.BooleanField(verbose_name="是否启用", default=True)
    level = models.ForeignKey(LevelModel,verbose_name="审批层次", on_delete=models.CASCADE)
    start_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "flow_tb"
        verbose_name = "流程表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.flow_name


# 流程节点
class FlowNodeModel(models.Model):
    # 审批类型 vettype 所属部门   true   流程flow
    STATE_OPTIONS = (
        (0, "未审批"), (1, "同意"), (2, "驳回")
    )
    vettype = models.ForeignKey(TypeModel, on_delete=models.CASCADE, verbose_name="所属类别")
    flow = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name="流程")
    name = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="发起人")
    node_name = models.CharField(verbose_name="流程节点名称", max_length=25)
    vettsatus = models.CharField(max_length=10, choices=STATE_OPTIONS, verbose_name="审批状态", default=0)
    examination_why = models.CharField(verbose_name="审批原因", max_length=1000)
    level = models.ForeignKey(LevelModel,verbose_name="当前节点(审批层次)", on_delete=models.CASCADE)
    why = models.CharField(verbose_name="请求", max_length=100)
    start_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    vettime = models.DateTimeField(verbose_name="审批时间", auto_now=True, null=True, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "flow_node_model"
        verbose_name = "流程节点表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 用户流程节点表
class UserFlowNodeModel(models.Model):
    # user_node_name = models.CharField(verbose_name="用户节点的名称", max_length=25)
    name = models.ForeignKey(UserModel, verbose_name="用户名", on_delete=models.CASCADE)
    flow = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name="流程节点")
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "user_flow_node_tb"
        verbose_name = "用户流程节点表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.name


# 节点信息表
class NodeInformationModel(models.Model):
    describe = models.CharField(verbose_name="描述", max_length=99)
    # flow = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name="流程")
    flow_node = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name="流程节点")
    is_through = models.BooleanField(verbose_name="是否通过", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "node_information_tb"
        verbose_name = "节点信息表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.describe


# 历史审批
class HistoryExaminationModel(models.Model):
    # flow = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name="发起流程")
    flow_node = models.ForeignKey(FlowNodeModel, on_delete=models.CASCADE, verbose_name="当前节点")
    # end_data = models.DateTimeField(verbose_name="时间", default=datetime.datetime.now())
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "history_examination_tb"
        verbose_name = "历史表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "发起人：%s, 发起流程：%s" % (self.flow_node.name, self.flow_node.name)


# 模板类型
class Template(BaseModel):
    template_name = models.CharField('模板名称', max_length=32)
    type = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='所属流程', null=True, blank=True)

    class Meta:
        db_table = 'tb_template'
        verbose_name = '模板类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.template_name


# 请假类型
class Leave_Type(BaseModel):
    type_name = models.CharField('请假类型', max_length=32)
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_leave_type'
        verbose_name = '请假类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.type_name


# 请假模板
class Leave(BaseModel):
    cause = models.CharField('请假原因', max_length=128)
    begin_time = models.DateField(verbose_name='请假日期', blank=True, null=True)
    end_time = models.DateField(verbose_name='归假日期', blank=True, null=True)
    leave_type = models.ForeignKey('Leave_Type', on_delete=models.CASCADE, verbose_name='请假类型', null=True, blank=True)
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_leave'
        verbose_name = '请假模板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cause


# 车型
class Vehicle(BaseModel):
    vehicle_name = models.CharField('车型名', max_length=32)
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_vehicle'
        verbose_name = '车型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.vehicle_name


# 车牌
class Plate(BaseModel):
    plate_name = models.CharField('车牌号', max_length=32)
    vehicle = models.ForeignKey('Vehicle', on_delete=models.CASCADE, verbose_name='所属车型')

    class Meta:
        db_table = 'tb_plate'
        verbose_name = '车牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.plate_name


# 借款模板
class Money(BaseModel):
    begin_time = models.DateField(verbose_name='借款日期', null=True, blank=True)
    cause = models.CharField('借款原因', max_length=128)
    money = models.IntegerField(verbose_name='借款金额')
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_money'
        verbose_name = '借款模板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cause


# 出差模板
class Travel(BaseModel):
    cause = models.CharField('出差原因', max_length=128)
    begin_time = models.DateField(verbose_name='出差日期', null=True, blank=True)
    end_time = models.DateField(verbose_name='结束日期', null=True, blank=True)
    address = models.CharField('目的地', max_length=32)
    peer = models.CharField('同行人', max_length=8)
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_travel'
        verbose_name = '出差模板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cause


# 转正模板
class Formal(BaseModel):
    begin_time = models.DateField(verbose_name='转正日期', null=True, blank=True)
    cause = models.CharField('自评', max_length=128)
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_formal'
        verbose_name = '转正模板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cause


# 用车模板
class Car(BaseModel):
    begin_time = models.DateField(verbose_name='借车日期', null=True, blank=True)
    end_time = models.DateField(verbose_name='还车日期', null=True, blank=True)
    start = models.CharField('起始地', max_length=32)
    end = models.CharField('目的地', max_length=32)
    vehicle = models.ForeignKey('Vehicle', on_delete=models.CASCADE, verbose_name='车型')
    plate = models.ForeignKey('Plate', on_delete=models.CASCADE, verbose_name='车牌')
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_car'
        verbose_name = '用车模板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.start


# 报销模板
class Expense(BaseModel):
    money = models.IntegerField(verbose_name='报销金额')
    cause = models.CharField('报销事由', max_length=256)
    voucher = models.CharField('报销图片', max_length=256)
    type = models.ForeignKey(Template, on_delete=models.CASCADE, verbose_name='模板类型')

    class Meta:
        db_table = 'tb_expense'
        verbose_name = '报销模板'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cause


# 材料
class StuffModel(BaseModel):
    name = models.CharField("材料名称", max_length=100)
    path = models.CharField("材料地址", max_length=256)

    class Meta:
        db_table = 'stuff_tb'
        verbose_name_plural = '材料表'

    def __str__(self):
        return self.name






